package com.gx.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gx.server.common.BaseResponse;
import com.gx.server.common.ErrorCode;
import com.gx.server.common.ResultUtils;
import com.gx.server.model.domain.ExamErrors;
import com.gx.server.model.domain.ExamRight;
import com.gx.server.model.domain.ExamScore;
import com.gx.server.model.domain.User;
import com.gx.server.model.dto.InfoDTO;
import com.gx.server.model.service.ExamErrorsService;
import com.gx.server.model.service.ExamRightService;
import com.gx.server.model.service.ExamScoreService;
import com.gx.server.model.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ExamRightService examRightService;

    @Autowired
    private ExamErrorsService examErrorsService;

    @Autowired
    private ExamScoreService examScoreService;

    /**
     * 返回所有用户信息
     * @return
     */
    @GetMapping("/all")
    public BaseResponse<List<User>> allUser(){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //查表
        List<User> list = userService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     * 用户注册
     * @param user
     * @param code
     * @return
     */
    @PostMapping("/Register")
    public  BaseResponse<User> Register(@RequestBody User user){
        log.info("新增用户，用户信息：{}",user.toString());

        User l = userService.userRegister(user.getUserAccount(), user.getPassword(), user.getEmail(), user.getCode());

        return ResultUtils.success(l);

    }


    /**
     * 用户登录
     * @param request
     * @param user
     * @return
     */
    @PostMapping("/Login")
    public BaseResponse<User> Login(HttpServletRequest request,@RequestBody User user){
        log.info("用户登录",user.toString());

        User user_login = userService.userLogin(user.getUserAccount(), user.getPassword(), request);

        return ResultUtils.success(user_login);

    }

    /**
     * 修改用户基础信息(名字，邮箱
     * @param user
     * @return
     */
    @PutMapping("/UpBaseUser")
    public BaseResponse<String>  UpBaseUser(@RequestBody User user){
        log.info(" 修改用户基础信息",user.toString());

        boolean flag = userService.updateUser(user.getId(), user);

        if (flag){
            return ResultUtils.success("修改成功");
        }else {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"修改失败");
        }


    }

    @GetMapping("/info")
    public BaseResponse<InfoDTO> infoData(Long id,int subject){
        QueryWrapper<ExamErrors> queryWrapper_errors = new QueryWrapper<>();
        QueryWrapper<ExamRight> queryWrapper_right = new QueryWrapper<>();
        QueryWrapper<ExamScore> queryWrapper_scors = new QueryWrapper<>();

        queryWrapper_errors.eq("user_id",id);
        queryWrapper_right.eq("user_id",id);
        queryWrapper_scors.eq("user_id",id);

        int countE = examErrorsService.count(queryWrapper_errors);
        int countR = examRightService.count(queryWrapper_right);
        List<ExamScore> list = examScoreService.list(queryWrapper_scors);
        InfoDTO infoDTO = new InfoDTO();


        if (list.size()>0){
            int sum =0;
            for (ExamScore examScore : list) {
                sum =sum + examScore.getScore();
            }
            double v = (double) sum / list.size();
            double Average = Math.round(v * 100.0) / 100.0;

            infoDTO.setTest_Average(Average);
            infoDTO.setNum_test(list.size());

        }else {
            infoDTO.setTest_Average(0);
            infoDTO.setNum_test(0);
        }

        double result = 0;
        infoDTO.setNum_questions(countE+countR);
        if (subject==1){
             result = (double) countE /2149;
        }else if (subject==4) {
             result = (double) countE /1663;
        }

        double roundedValue = Math.round(result * 100.0) / 100.0;
        infoDTO.setAccuracy(roundedValue);

        return ResultUtils.success(infoDTO);

    }

    @GetMapping("/examOK")
    public void examOK(Long id,int score){
        ExamScore examScore = new ExamScore();
        examScore.setScore(score);
        examScore.setUserId(id);
        examScoreService.save(examScore);
    }


}
